package com.lineage.server.serverpackets;

import com.lineage.server.datatables.lock.BuddyReading;
import com.lineage.server.model.Instance.L1DeInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.templates.L1BuddyTmp;
import com.lineage.server.world.World;
import com.lineage.server.world.WorldDe;
import java.util.ArrayList;
import java.util.Iterator;

public class S_Buddy extends ServerBasePacket {
  private static final String _buddy = "buddy";
  
  private byte[] _byte;
  
  public S_Buddy(int objId) {
    this._byte = null;
    buildPacket(objId);
  }
  
  private void buildPacket(int objId) {
    writeC(39);
    writeD(objId);
    writeS("buddy");
    writeH(2);
    writeH(2);
    String result = new String("");
    String onlineBuddy = new String("");
    ArrayList<L1BuddyTmp> list = BuddyReading.get().userBuddy(objId);
    if (list != null) {
      Iterator<L1BuddyTmp> iterator = BuddyReading.get().userBuddy(objId).iterator();
      while (iterator.hasNext()) {
        L1BuddyTmp buddyTmp = iterator.next();
        String buddy_name = buddyTmp.get_buddy_name();
        result = String.valueOf(String.valueOf(result)) + buddy_name + " ";
        L1PcInstance find = World.get().getPlayer(buddy_name);
        if (find != null)
          onlineBuddy = String.valueOf(String.valueOf(onlineBuddy)) + find.getName() + " "; 
        L1DeInstance de = WorldDe.get().getDe(buddy_name);
        if (de != null)
          onlineBuddy = String.valueOf(String.valueOf(onlineBuddy)) + de.getNameId() + " "; 
      } 
    } 
    writeS(result);
    writeS(onlineBuddy);
  }
  
  public byte[] getContent() {
    if (this._byte == null)
      this._byte = getBytes(); 
    return this._byte;
  }
  
  public String getType() {
    return getClass().getSimpleName();
  }
}
